package viroyal.com.base.activity.engineering;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.suntiago.network.network.ErrorCode;
import com.suntiago.network.network.rsp.BaseResponse;

import rx.functions.Action1;
import viroyal.com.base.R;
import viroyal.com.base.activity.main.MainDelegate;
import viroyal.com.base.activity.main.MainModel;
import viroyal.com.base.net.rsp.DistributeSchoolResponseS;
import viroyal.com.base.net.rsp.GetSchoolListResponseS;
import viroyal.com.dev.NFCMonitorBaseActivity;
import viroyal.com.dev.splash.ConfigDevice;

public class EngineeringActivity extends NFCMonitorBaseActivity<EngineeringDelegate, EngineeringModel> {
  private String UUID;
  private String mSchoolName = "远御中学";

  @Override
  protected Class<EngineeringDelegate> getDelegateClass() {
    return EngineeringDelegate.class;
  }

  @Override
  protected Class<EngineeringModel> getModelClass() {
    return EngineeringModel.class;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    super.initView(savedInstanceState);
    viewDelegate.initView();
    UUID = viewDelegate.getDeviceUUID();
    getSchoolList();
  }

  /**
   * 获取学校列表
   */
  private void getSchoolList() {
    addRxSubscription(iModel.getSchoolList(getSchoolListResponseS -> {
      if (getSchoolListResponseS.error_code == ErrorCode.SUCCESS) {
        viewDelegate.updateListData(getSchoolListResponseS.extra.data).setOnItemClickListener((adapterView, view, i, l) -> showDialog(getSchoolListResponseS.extra.data.get(i).unit_name, getSchoolListResponseS.extra.data.get(i).unit_id));
      } else {
        Toast.makeText(this, getSchoolListResponseS.extra.message, Toast.LENGTH_SHORT).show();
      }
    }));
    addRxSubscription(iModel.getDeviceInfo(ConfigDevice.getDeviceId(), getDeviceInfoResponseS -> {
      if (getDeviceInfoResponseS.error_code == ErrorCode.SUCCESS) {
        mSchoolName = getDeviceInfoResponseS.extra.school_name;
      }

    }));
  }

  private void showDialog(String schoolName, String schoolId) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("分配学校");
    builder.setMessage("当前设备已经分配给[" +mSchoolName+ "],是否要将该设备分配给" + schoolName);
    // 为对话框设置取消按钮
    builder.setNegativeButton("取消", (arg0, arg1) -> {
      arg0.dismiss();
    });
    // 为对话框设置确定按钮
    builder.setPositiveButton("确定", (arg0, arg1) -> {
      addRxSubscription(iModel.distributeSchool(UUID, schoolId, distributeSchoolResponseS -> {
        arg0.dismiss();
        if ( distributeSchoolResponseS.error_code == ErrorCode.SUCCESS) {
          Toast.makeText(EngineeringActivity.this, "分配成功", Toast.LENGTH_SHORT).show();
          ConfigDevice.school_id =  schoolId;
          this.finish();
        } else {
          Toast.makeText(this, distributeSchoolResponseS.extra.message, Toast.LENGTH_SHORT).show();
        }
      }));
    });
    builder.create().show();// 使用show()方法显示对话框

  }
}
